package com.tesisutn.restsoft.resultado.formatter;

import com.tesisutn.restsoft.resultado.*;

/**
 *
 * @author Usuario
 */
public class HTMLSelectFormatter extends ContentFormatter {

    @Override
    protected String formatContent(Content content) {
        StringBuilder stringResult = new StringBuilder();
        stringResult.append("\"");
        if (content.size() == 0) {
            stringResult.append("<option selected disabled value=''> No hay elementos cargados </option >");
        } else {
            String primeraFilaSeleccionada = (hasSelectedRows(content))
                    ? ""
                    : "selected";
            stringResult.append(String.format("<option %s disabled value=''> Seleccione un elemento </option >", primeraFilaSeleccionada));
            String isSelected;
            String id;
            String texto;
            for (Row row : content.getRows()) {
                isSelected = (row.isSelected()) ? "selected" : "";
                id = row.getValueAt(0);
                texto = row.getValueAt(1);
                stringResult.append(String.format("<option %s value = '%s'>%s</option >", isSelected, id, texto));
            }
        }
        stringResult.append("\"");
        return stringResult.toString();
    }

    private boolean hasSelectedRows(Content content) {
        for(Row row : content.getRows()){
            if (row.isSelected()) {
                return true;
            }
        }
        return false;
    }
}
